import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { LinkItem, OrderItem, CommissionRecord } from '@/types/distributor'
import { 
  generatePaymentLinksData, 
  generateOrdersData, 
  generateCommissionDetailsData,
  generateLinkDetailData,
  generateOrderDetailData,
  generateWithdrawalRecordsData
} from '@/utils/mockData'

export const useDistributorStore = defineStore('distributor', () => {
  // 支付链接列表
  const paymentLinks = ref<LinkItem[]>([])
  
  // 订单列表
  const orders = ref<OrderItem[]>([])
  
  // 佣金记录
  const commissionRecords = ref<CommissionRecord[]>([])
  
  // 统计数据
  const stats = ref({
    totalLinks: 0,
    activeLinks: 0,
    totalOrders: 0,
    totalCommission: 0,
    pendingCommission: 0,
    withdrawableCommission: 0
  })
  
  // 计算属性: 活跃链接数量
  const activeLinksCount = computed(() => {
    return paymentLinks.value.filter(link => link.isActive).length
  })
  
  // 计算属性: 总佣金金额
  const totalCommission = computed(() => {
    return commissionRecords.value.reduce((total, record) => total + record.amount, 0)
  })
  
  // 计算属性: 可提现佣金金额
  const withdrawableCommission = computed(() => {
    return commissionRecords.value
      .filter(record => record.status === 'withdrawable')
      .reduce((total, record) => total + record.amount, 0)
  })
  
  // 方法: 加载支付链接
  async function loadPaymentLinks() {
    try {
      // 这里将来会实现API调用
      // 暂时使用mock数据
      const data = generatePaymentLinksData(20)
      paymentLinks.value = data
      return data
    } catch (error) {
      console.error('加载支付链接失败', error)
      return []
    }
  }
  
  // 方法: 获取链接详情
  async function getLinkDetail(linkId: string) {
    try {
      // 这里将来会实现API调用
      // 暂时使用mock数据
      return generateLinkDetailData(linkId)
    } catch (error) {
      console.error('获取链接详情失败', error)
      return null
    }
  }
  
  // 方法: 创建新的支付链接
  async function createPaymentLink(linkData: Partial<LinkItem>) {
    try {
      // 这里将来会实现API调用
      // 暂时使用空实现
      console.log('创建支付链接', linkData)
      return true
    } catch (error) {
      console.error('创建支付链接失败', error)
      return false
    }
  }
  
  // 方法: 加载订单数据
  async function loadOrders() {
    try {
      // 这里将来会实现API调用
      // 暂时使用mock数据
      const data = generateOrdersData(30)
      orders.value = data
      return data
    } catch (error) {
      console.error('加载订单数据失败', error)
      return []
    }
  }

  // 方法: 获取订单详情
  async function getOrderDetail(orderId: string) {
    try {
      // 这里将来会实现API调用
      // 暂时使用mock数据
      return generateOrderDetailData(orderId)
    } catch (error) {
      console.error('获取订单详情失败', error)
      return null
    }
  }
  
  // 方法: 加载佣金明细
  async function loadCommissionDetails(params = {}) {
    try {
      // 这里将来会实现API调用
      // 暂时使用mock数据
      return generateCommissionDetailsData(params)
    } catch (error) {
      console.error('加载佣金明细失败', error)
      return []
    }
  }

  // 方法: 加载提现记录
  async function loadWithdrawalRecords(params = {}) {
    try {
      // 这里将来会实现API调用
      // 暂时使用mock数据
      return generateWithdrawalRecordsData(params)
    } catch (error) {
      console.error('加载提现记录失败', error)
      return []
    }
  }
  
  // 方法: 申请提现
  async function requestWithdrawal(amount: number) {
    try {
      // 这里将来会实现API调用
      // 暂时使用模拟延迟
      await new Promise(resolve => setTimeout(resolve, 1000))
      console.log('申请提现', amount)
      return true
    } catch (error) {
      console.error('申请提现失败', error)
      return false
    }
  }
  
  return {
    paymentLinks,
    orders,
    commissionRecords,
    stats,
    activeLinksCount,
    totalCommission,
    withdrawableCommission,
    loadPaymentLinks,
    getLinkDetail,
    createPaymentLink,
    loadOrders,
    getOrderDetail,
    loadCommissionDetails,
    loadWithdrawalRecords,
    requestWithdrawal
  }
}) 